/* 
 * Distributed as part of jdiff
 * 
 * Copyright (C) 2008 QArks.com
 *
 * Author: Pierre Meyer <support@qarks.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1, as 
 * published by the Free Software Foundation.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; see the file LICENSE.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */
package diff.tela;

import java.io.*;
import java.util.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class DiffInputDialog extends JDialog implements ActionListener, BrowseListener {

	private JButton ok,cancel;
	private JTextField leftFile,rightFile;
	private boolean cancelled = false;
	private String defaultPath = System.getProperty("user.home");
	private BrowseButton leftBrowse,rightBrowse;

	public DiffInputDialog(Dialog dialog,String leftFilePath,String rightFilePath) {
		super(dialog,"Diff input",true);
		init(leftFilePath,rightFilePath);
	}

	public DiffInputDialog(Frame frame,String leftFilePath,String rightFilePath) {
		super(frame,"Diff input",true);
		init(leftFilePath,rightFilePath);
	}

	private void init(String leftFilePath,String rightFilePath){
		Container cont = getContentPane();
		cont.setLayout(new GridLayout(1,1));


		JPanel labels = new JPanel(new GridLayout(2,1,10,10));
		labels.add(new JLabel("Left file: "),0);
		labels.add(new JLabel("Right file: "),1);
		JPanel values = new JPanel(new GridLayout(2,1,10,10));
		leftFile = new JTextField(32);
		rightFile = new JTextField(32);
		values.add(leftFile,0);
		values.add(rightFile,1);
		JPanel browsePanel = new JPanel(new GridLayout(2,1,10,10));
		leftBrowse = new BrowseButton(leftFile,defaultPath,this);
		rightBrowse = new BrowseButton(rightFile,defaultPath,this);
		browsePanel.add(leftBrowse,0);
		browsePanel.add(rightBrowse,1);

		loadPath();
		if (leftFilePath!=null){
			leftFile.setText(leftFilePath);
		}
		if (rightFilePath!=null){
			rightFile.setText(rightFilePath);
		}
		leftFile.addKeyListener(new KeyAdapter(){
			public void keyReleased(KeyEvent e){
				checkInput();
			}
		});
		rightFile.addKeyListener(new KeyAdapter(){
			public void keyReleased(KeyEvent e){
				checkInput();
			}
		});

		JPanel panel = new JPanel(new BorderLayout(5,5));
		panel.add(labels,BorderLayout.WEST);
		panel.add(values,BorderLayout.CENTER);
		panel.add(browsePanel,BorderLayout.EAST);

		JPanel toolbar = new JPanel(new FlowLayout(FlowLayout.RIGHT,5,5));
		ok = new JButton("Ok");
		cancel = new JButton("Cancel");
		ok.addActionListener(this);
		cancel.addActionListener(this);
		checkInput();
		toolbar.add(ok);
		toolbar.add(cancel);

		JPanel mainPanel = new JPanel(new BorderLayout(5,5));
		mainPanel.add(panel,BorderLayout.CENTER);
		mainPanel.add(toolbar,BorderLayout.SOUTH);
		mainPanel.setBorder(new EmptyBorder(10,10,10,10));

		cont.add(mainPanel);
		pack();
	}

	public void onBrowsed(String path) {
		File file = new File(path);
		if (file.exists()){
			defaultPath = file.getParentFile().getAbsolutePath();
			leftBrowse.setCurrentPath(defaultPath);
			rightBrowse.setCurrentPath(defaultPath);
		}
		checkInput();
	}

	private void checkInput(){
		ok.setEnabled(getLeftFilePath().length()>0 && getRightFilePath().length()>0);
	}

	public boolean isCancelled(){
		return cancelled;
	}

	public String getLeftFilePath(){
		return leftFile.getText();
	}

	public String getRightFilePath(){
		return rightFile.getText();
	}

	private void loadPath(){
		String path = System.getProperty("user.home")+File.separator+".sourcemanager";
		File dir = new File(path);
		if (dir.exists()){
			FileInputStream fis = null;
			try{
				File file = new File(dir,"diff.properties");
				if (file.exists()){
					fis = new FileInputStream(file);
					Properties props = new Properties();
					props.load(fis);
					leftFile.setText(props.getProperty("left", ""));
					rightFile.setText(props.getProperty("right", ""));
					defaultPath = props.getProperty("defaultPath", System.getProperty("user.home"));
					leftBrowse.setCurrentPath(defaultPath);
					rightBrowse.setCurrentPath(defaultPath);
				}
			}
			catch(Exception ex){
				ex.printStackTrace();
			}
			finally{
				if (fis!=null){
					try{fis.close();}catch(Exception ex){ex.printStackTrace();}
				}
			}
		}
	}

	private void savePath(){
		String path = System.getProperty("user.home")+File.separator+".sourcemanager";
		File dir = new File(path);
		if (dir.exists()==false){
			dir.mkdirs();
		}
		Properties props = new Properties();
		props.put("left",getLeftFilePath());
		props.put("right",getRightFilePath());
		props.put("defaultPath",defaultPath);
		File saved = new File(dir,"diff.properties");
		FileOutputStream fos = null;
		try{
			fos=new FileOutputStream(saved);
			props.store(fos,"");
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		finally{
			if (fos!=null){
				try{fos.close();}catch(Exception ex){ex.printStackTrace();}
			}
		}
	}

	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == ok){
			Object options[] = new Object[]{"Ok"};
			if ((new File(getLeftFilePath())).exists()==false){
				JOptionPane.showOptionDialog(this,
						"file '"+getLeftFilePath()+"' is not found",
						"Open file",
						JOptionPane.DEFAULT_OPTION,
						JOptionPane.ERROR_MESSAGE,
						null, options, options[0]);
				return;
			}
			if ((new File(getRightFilePath())).exists()==false){
				JOptionPane.showOptionDialog(this,
						"file '"+getRightFilePath()+"' is not found",
						"Open file",
						JOptionPane.DEFAULT_OPTION,
						JOptionPane.ERROR_MESSAGE,
						null, options, options[0]);
				return;
			}
			savePath();
			dispose();
		}
		else{
			cancelled = true;
			dispose();
		}
	}
}
